package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yangeit
 * @date 2023年11月27日 9:52
 * @Description 定时任务
 */
@Component
@Slf4j
public class OrderTask {
    int index=0;
////
//    @Scheduled(cron = "0,6,37,55 * * * * ? ")
//    public void task1(){
//        index++;
//        log.info("{}，自动执行：{}",index, LocalDateTime.now());
////        查询订单列表，将过时的订单改成取消
//    }

    @Autowired
    OrderMapper orderMapper;

    /**
     * 处理支付超时的订单
     */
    @Scheduled(cron = "0 * * * * ?")
    public void  processTimeOutOrder(){
        log.info("处理支付超时的订单：{}",LocalDateTime.now());


//        1. 查询订单，条件：状态=未支付，下单时间
//       下单时间 =当前时间-15分钟
         LocalDateTime ordertime = LocalDateTime.now().plusMinutes(-15);

      List<Orders> orders=orderMapper.getOrderByStatusAndOrderTime(Orders.PENDING_PAYMENT,ordertime);
      if (orders!=null && orders.size()>0){
          for (Orders order : orders) {

              order.setStatus(Orders.CANCELLED);
              order.setCancelReason("支付超时，自动取消");
              order.setCancelTime(LocalDateTime.now());
              log.info("支付超时，自动取消,订单id：{}",order.getId());
              orderMapper.update(order);


          }


      }

    }
    /**
     * 处理12点之前未支付的订单
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void  processDayOutOrder(){
        log.info("理12点之前未支付的订单：{}",LocalDateTime.now());

        List<Orders> orders=orderMapper.getOrderByStatusAndOrderTime(Orders.PENDING_PAYMENT,null);
        if (orders!=null && orders.size()>0){
            for (Orders order : orders) {
                order.setStatus(Orders.CANCELLED);
                order.setCancelReason("店铺打烊，不接单，自动取消");
                order.setCancelTime(LocalDateTime.now());
                log.info("支付超时，自动取消,订单id：{}",order.getId());
                orderMapper.update(order);
            }


        }

    }


    /**
     * 处理派送中的订单，每天的1点整 ，将派送中的改成已完成
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void  processFinshedOrder(){
        log.info("处理派送中的订单，每天的1点整 ，将派送中的改成已完成：{}",LocalDateTime.now());
         LocalDateTime now = LocalDateTime.now().plusMinutes(-60);

        List<Orders>  orders= orderMapper.getOrderByStatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS,now);

        if (orders!=null && orders.size()>0){

            for (Orders order : orders) {


                order.setStatus(Orders.COMPLETED);
                log.info("自动设置订单已完成,订单id：{}",order.getId());
                orderMapper.update(order);

            }
        }

    }

//    @Autowired
//    WebSocketServer webSocketServer;

//    @Scheduled(cron = "0/2 * * * * ?")
//    public void sendMessage2Client(){
//
//        webSocketServer.sendToAllClient("这是来自服务器的消息:"+LocalDateTime.now());
//    }





}
